<?php
function recursionCalculateCombination($index_bit_one_array,&$data){
	$return = array();
	$row = 0;
	if (count($index_bit_one_array)==1){
		$index_bit_one = array_shift($index_bit_one_array);
		foreach ($data[$index_bit_one] as $vaule){
			$return[][] = $vaule;
		}
	}
	while (!empty($index_bit_one_array)){
		$index_bit_one = array_shift($index_bit_one_array);
		$retult = array_merge(recursionCalculateCombination($index_bit_one_array,$data));
		foreach ($data[$index_bit_one] as $value){
			foreach ($retult as $r){
				$return[] = array_merge(array($value),$r);
			}
		}
	}
	return $return;
}
echo '<h3>Product Attributes Sitemap</h3>';
if($sitemapXML->SitemapOpen('attributes_products')){
	$sql ='SELECT DISTINCT(pov.products_options_values_id),po.products_options_id FROM '.TABLE_PRODUCTS_OPTIONS.' po JOIN '.
		  TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS.' po2pov ON po.products_options_id=po2pov.products_options_id JOIN '.
		  TABLE_PRODUCTS_OPTIONS_VALUES.' pov ON pov.products_options_values_id=po2pov.products_options_values_id '.
		  'WHERE po.language_id=pov.language_id AND po.language_id='. $sitemapXML->default_language_id.' '.
		  'ORDER BY po.products_options_id ASC,pov.products_options_values_id ASC';
	$r = $db->Execute($sql);
	//echo $sql;
	$result = array();
	while (!$r->EOF){
		$result[$r->fields['products_options_id']][$r->fields['products_options_values_id']] = $r->fields['products_options_values_id'];
		$r->MoveNext();
	}
	$key_array = array_keys($result);
	$count_options = count($result);
	$loop = pow(2,$count_options)-1;
	$combinations = array();//组合方式排列
	$combinations_final = array();//最终的组合方式,用于生成LINK
	for ($i=1;$i<=$loop;$i++){
		$combinations[] = decbin($i); 
	}	
	foreach ($combinations as $combination) {
		$combination =(string)$combination;
		$bit_array = array_reverse(str_split($combination));
		$index_bit_one_array = array();//所在位为1的索引
		foreach ($bit_array as $index=>$one_zero){
			if ($one_zero=='1'){
				$index_bit_one_array[] = $key_array[$index];
			}
		}
		$combinations_final = array_merge($combinations_final,recursionCalculateCombination($index_bit_one_array,$result));
	}
	$row = 1;
	//echo count($combinations_final);
	$listing_sql = "select count(*) as total from (select p.products_id from " . TABLE_PRODUCTS . " p " .
	       "join ".TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p2c.products_id=p.products_id ".
	       "join ".TABLE_PRODUCTS_DESCRIPTION." pd on pd.products_id=p.products_id ".
	       "join ".TABLE_CATEGORIES." c on c.categories_id=p2c.categories_id ".
	 	   "join ".TABLE_PRODUCTS_ATTRIBUTES . " pa on pa.products_id=p.products_id ". 
	       "where p.products_status = 1
	         and c.categories_status = 1 
	         and pd.language_id = '" . (int)$sitemapXML->default_language_id . "'
	         and pa.options_values_id in(%s) group by p.products_id having count(*)>=%s) as t";
	foreach ($combinations_final as $v) {
		$sql = sprintf($listing_sql,implode(',', $v),count($v));
		$r = $db->Execute($sql);
		if ($r->fields['total']>0){
			$sitemapXML->writeItem(FILENAME_DEFAULT, 'options_values_id='.implode('_', $v) , $sitemapXML->default_language_id);
		}
//		if ($row++==100) break;
	}
	$sitemapXML->SitemapClose();
}